package br.com.minhascontas.activity.relatorio;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import br.com.example.minhascontas.R;
import br.com.minhascontas.DAO.MinhasContasOpenHelper;
import br.com.minhascontas.model.Grupo;

public class RelatorioPorGrupo extends Activity{
	
	private MinhasContasOpenHelper dataSource = new MinhasContasOpenHelper(this);
	private Grupo clsGrupo = new Grupo();
	private int intIdGrupo;
	
	private Spinner spGrupo;
	private Button btnExibirRelatorioPorGrupo;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.relatorio_por_grupo);
		
		spGrupo = (Spinner) findViewById(R.id.spGrupo);
		btnExibirRelatorioPorGrupo = (Button) findViewById(R.id.btnExibirRelatorioPorGrupo);
		
		populaSpinnerGrupo();
		
		spGrupo.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View view,
					int pos, long id) {
				// TODO Auto-generated method stub
				clsGrupo = (Grupo) adapter.getItemAtPosition(pos);
				intIdGrupo = (int) (long) clsGrupo.getId();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btnExibirRelatorioPorGrupo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				String strIdGrupo = Integer.toString(intIdGrupo);
				Intent intent = new Intent(RelatorioPorGrupo.this, ExibirLancamentos.class);
				intent.putExtra("grupo", strIdGrupo);
				startActivity(intent);
			}
		});
		
	}
	
	private void populaSpinnerGrupo() {
		List<Grupo> labels = dataSource.listarGrupo();

		ArrayAdapter<Grupo> dataAdapter = new ArrayAdapter<Grupo>(this,
				android.R.layout.simple_list_item_1, labels);

		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spGrupo.setAdapter(dataAdapter);
		dataSource.close();
	}
}
